//
//  NSDate+XHLKit.m
//  XHLKit
//
//  Created by qinyulun on 16/6/18.
//  Copyright © 2016年 leTian. All rights reserved.
//

#import "NSDate+XHLKit.h"

@implementation NSDate (XHLKit)

+ (NSString*)stringWithTime:(NSString *)timeString formatter:(NSString *)formatter
{
    return [[self alloc]commonInitWithMethod:timeString formatString:formatter];
}

+ (NSString*)stringWithTime:(NSString *)timeString dateType:(DateType )dateType
{
    switch (dateType) {
        case DateTypeOne:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"yyyy/MM/dd"];
            break;
            
        case DateTypeTwo:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"yyyy/M/d"];
            break;
            
        case DateTypeThree:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"HH:mm:ss"];
            break;
            
        case DateTypeFour:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"hh:mm:ss"];
            break;
            
        case DateTypeFive:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"yyyy/MM/dd HH:mm:ss"];
            break;
            
        case DateTypeSix:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"yyyy-MM-dd"];
            break;
            
        case DateTypeSeven:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"yyyy-M-d"];
            break;
            
        case DateTypeEight:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"HH:mm"];
            break;
            
        case DateTypeNine:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"hh:mm"];
            break;
            
        case DateTypeTen:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"yyyy-M-d  HH:mm"];
            break;
            
        case DateTypeEleven:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"M-d  hh:mm"];
            break;
            
        case DateTypeTwelve:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"M-d  HH:mm"];
            break;
            
        case DateTypeThirteen:
            return [[self alloc] commonInitWithMethod:timeString formatString:@"M月d日  HH:mm"];
            break;
            
        default:
            break;
    }
    
    return nil;
}

- (NSString *)commonInitWithMethod:(NSString *)timeString formatString:(NSString *)formatString
{
    NSString* timeStr = timeString;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH.mm.ss.sss"];//设置源时间字符串的格式
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Guizhou"];//设置时区
    
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:timeStr];//将源时间字符串转化为NSDate
    
    NSDateFormatter* toformatter = [[NSDateFormatter alloc] init];
    
    [toformatter setDateStyle:NSDateFormatterMediumStyle];
    
    [toformatter setTimeStyle:NSDateFormatterShortStyle];
    
    [toformatter setDateFormat:formatString];//设置目标时间字符串的格式
    
    NSString *targetTime = [toformatter stringFromDate:date];//将时间转化成目标时间字符串
    
    return targetTime;
}


+ (NSDate *)yesterday {
    XHLDateInformation inf = [[NSDate date] dateInformation];
    inf.day--;
    return [self dateFromDateInformation:inf];
}

+ (NSDate *)month {
    return [[NSDate date] month];
}

- (NSDate *)month {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comp =
    [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth)
                 fromDate:self];
    [comp setDay:1];
    NSDate *date = [gregorian dateFromComponents:comp];
    return date;
}

- (NSInteger)weekday {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps =
    [gregorian components:(NSCalendarUnitDay | NSCalendarUnitMonth |
                           NSCalendarUnitYear | NSCalendarUnitWeekday)
                 fromDate:self];
    NSInteger weekday = [comps weekday];
    return weekday;
}

- (NSString *)dayFromWeekday {
    switch ([self weekday]) {
        case 1:
            return NSLocalizedStringFromTable(@"SUNDAY", @"BFKit", @"");
            break;
        case 2:
            return NSLocalizedStringFromTable(@"MONDAY", @"BFKit", @"");
            break;
        case 3:
            return NSLocalizedStringFromTable(@"TUERSDAY", @"BFKit", @"");
            break;
        case 4:
            return NSLocalizedStringFromTable(@"WEDNESDAY", @"BFKit", @"");
            break;
        case 5:
            return NSLocalizedStringFromTable(@"THURSDAY", @"BFKit", @"");
            break;
        case 6:
            return NSLocalizedStringFromTable(@"FRIDAY", @"BFKit", @"");
            break;
        case 7:
            return NSLocalizedStringFromTable(@"SATURDAY", @"BFKit", @"");
            break;
        default:
            break;
    }
    
    return @"";
}

- (NSDate *)timelessDate {
    NSDate *day = self;
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comp = [gregorian
                              components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                              fromDate:day];
    return [gregorian dateFromComponents:comp];
}

- (NSDate *)monthlessDate {
    NSDate *day = self;
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comp =
    [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth)
                 fromDate:day];
    return [gregorian dateFromComponents:comp];
}

- (BOOL)isSameDay:(NSDate *)anotherDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components1 = [calendar
                                     components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                     fromDate:self];
    NSDateComponents *components2 = [calendar
                                     components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                     fromDate:anotherDate];
    return ([components1 year] == [components2 year] &&
            [components1 month] == [components2 month] &&
            [components1 day] == [components2 day]);
}

- (NSInteger)monthsBetweenDate:(NSDate *)toDate {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *components = [gregorian components:NSCalendarUnitMonth
                                                fromDate:[self monthlessDate]
                                                  toDate:[toDate monthlessDate]
                                                 options:0];
    NSInteger months = [components month];
    return abs((int)months);
}

- (NSInteger)daysBetweenDate:(NSDate *)toDate {
    NSTimeInterval time = [self timeIntervalSinceDate:toDate];
    return (time / 60 / 60 / 24);
}

- (NSInteger)daysBetweenDateABS:(NSDate *)toDate {
    NSTimeInterval time = [self timeIntervalSinceDate:toDate];
    return fabs(time / 60 / 60 / 24);
}

- (BOOL)isToday {
    return [self isSameDay:[NSDate date]];
}

- (NSDate *)dateByAddingDays:(NSUInteger)days {
    NSDateComponents *c = [[NSDateComponents alloc] init];
    c.day = days;
    return [[NSCalendar currentCalendar] dateByAddingComponents:c
                                                         toDate:self
                                                        options:0];
}

+ (NSDate *)dateWithDatePart:(NSDate *)aDate andTimePart:(NSDate *)aTime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy"];
    NSString *datePortion = [dateFormatter stringFromDate:aDate];
    
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *timePortion = [dateFormatter stringFromDate:aTime];
    
    [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
    NSString *dateTime =
    [NSString stringWithFormat:@"%@ %@", datePortion, timePortion];
    return [dateFormatter dateFromString:dateTime];
}

- (NSString *)monthString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM"];
    return [dateFormatter stringFromDate:self];
}

+ (NSString *)monthStringWithMonthNumber:(NSInteger)month {
    switch (month) {
        case 1:
            return NSLocalizedStringFromTable(@"JANUARY", @"BFKit", @"");
            break;
        case 2:
            return NSLocalizedStringFromTable(@"FEBRUARY", @"BFKit", @"");
            break;
        case 3:
            return NSLocalizedStringFromTable(@"MARCH", @"BFKit", @"");
            break;
        case 4:
            return NSLocalizedStringFromTable(@"APRIL", @"BFKit", @"");
            break;
        case 5:
            return NSLocalizedStringFromTable(@"MAY", @"BFKit", @"");
            break;
        case 6:
            return NSLocalizedStringFromTable(@"JUNE", @"BFKit", @"");
            break;
        case 7:
            return NSLocalizedStringFromTable(@"JULY", @"BFKit", @"");
            break;
        case 8:
            return NSLocalizedStringFromTable(@"AUGUST", @"BFKit", @"");
            break;
        case 9:
            return NSLocalizedStringFromTable(@"SEPTEMBER", @"BFKit", @"");
            break;
        case 10:
            return NSLocalizedStringFromTable(@"OCTOBER", @"BFKit", @"");
            break;
        case 11:
            return NSLocalizedStringFromTable(@"NOVEMBER", @"BFKit", @"");
            break;
        case 12:
            return NSLocalizedStringFromTable(@"DECEMBER", @"BFKit", @"");
            break;
        default:
            return nil;
            break;
    }
}

- (NSString *)yearString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy"];
    return [dateFormatter stringFromDate:self];
}

- (XHLDateInformation)dateInformationWithTimeZone:(NSTimeZone *)timezone {
    XHLDateInformation info;
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [gregorian setTimeZone:timezone];
    NSDateComponents *comp =
    [gregorian components:(NSCalendarUnitMonth | NSCalendarUnitMinute |
                           NSCalendarUnitYear | NSCalendarUnitDay |
                           NSCalendarUnitWeekday | NSCalendarUnitHour |
                           NSCalendarUnitSecond)
                 fromDate:self];
    info.day = [comp day];
    info.month = [comp month];
    info.year = [comp year];
    
    info.hour = [comp hour];
    info.minute = [comp minute];
    info.second = [comp second];
    
    info.weekday = [comp weekday];
    
    return info;
}

- (XHLDateInformation)dateInformation {
    XHLDateInformation info;
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comp =
    [gregorian components:(NSCalendarUnitMonth | NSCalendarUnitMinute |
                           NSCalendarUnitYear | NSCalendarUnitDay |
                           NSCalendarUnitWeekday | NSCalendarUnitHour |
                           NSCalendarUnitSecond)
                 fromDate:self];
    info.day = [comp day];
    info.month = [comp month];
    info.year = [comp year];
    
    info.hour = [comp hour];
    info.minute = [comp minute];
    info.second = [comp second];
    
    info.weekday = [comp weekday];
    
    return info;
}

+ (NSDate *)dateFromDateInformation:(XHLDateInformation)info
                           timeZone:(NSTimeZone *)timezone {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [gregorian setTimeZone:timezone];
    NSDateComponents *comp =
    [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth)
                 fromDate:[NSDate date]];
    
    [comp setDay:info.day];
    [comp setMonth:info.month];
    [comp setYear:info.year];
    [comp setHour:info.hour];
    [comp setMinute:info.minute];
    [comp setSecond:info.second];
    [comp setTimeZone:timezone];
    
    return [gregorian dateFromComponents:comp];
}

+ (NSDate *)dateFromDateInformation:(XHLDateInformation)info {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comp =
    [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth)
                 fromDate:[NSDate date]];
    
    [comp setDay:info.day];
    [comp setMonth:info.month];
    [comp setYear:info.year];
    [comp setHour:info.hour];
    [comp setMinute:info.minute];
    [comp setSecond:info.second];
    //[comp setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    
    return [gregorian dateFromComponents:comp];
}

+ (NSString *)dateInformationDescriptionWithInformation:
(XHLDateInformation)info {
    return [NSString stringWithFormat:@"%02li/%02li/%04li %02li:%02li:%02li",
            (long)info.month, (long)info.day,
            (long)info.year, (long)info.hour,
            (long)info.minute, (long)info.second];
}

- (NSString *)date2String:(NSString *)format {
    NSDateFormatter *timeFormatter = [NSDateFormatter new];
    [timeFormatter setDateStyle:NSDateFormatterFullStyle];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    [timeFormatter setLocale:locale];
    timeFormatter.dateFormat = format;
    return [timeFormatter stringFromDate:self];
}

- (NSString*)weekDayStr:(NSString *)format
{
    NSString *weekDayStr = nil;
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    //    NSString *str = [self description];
    if (format.length >= 10) {
        NSString *nowString = [format substringToIndex:10];
        NSArray *array = [nowString componentsSeparatedByString:@"-"];
        if (array.count == 0) {
            array = [nowString componentsSeparatedByString:@"/"];
        }
        if (array.count >= 3) {
            NSInteger year = [[array objectAtIndex:0] integerValue];
            NSInteger month = [[array objectAtIndex:1] integerValue];
            NSInteger day = [[array objectAtIndex:2] integerValue];
            [comps setYear:year];
            [comps setMonth:month];
            [comps setDay:day];
        }
    }else{
        NSLog(@"请传入时间戳");
    }
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *_date = [gregorian dateFromComponents:comps];
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:_date];
    NSInteger week = [weekdayComponents weekday];
    //    week ++;
    switch (week) {
        case 1:
            weekDayStr = @"周日";
            break;
        case 2:
            weekDayStr = @"周一";
            break;
        case 3:
            weekDayStr = @"周二";
            break;
        case 4:
            weekDayStr = @"周三";
            break;
        case 5:
            weekDayStr = @"周四";
            break;
        case 6:
            weekDayStr = @"周五";
            break;
        case 7:
            weekDayStr = @"周六";
            break;
        default:
            weekDayStr = @"";
            break;
    }
    return weekDayStr;
}


-(NSString *)compareDate:(NSString *)date{
    
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *tomorrow, *yesterday;
    
    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    
    // 10 first characters of description is the calendar date:
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
    
    NSString * dateString = [date substringToIndex:10];
    
    if ([dateString isEqualToString:todayString])
    {
        return @"今天";
    } else if ([dateString isEqualToString:yesterdayString])
    {
        return @"昨天";
    }else if ([dateString isEqualToString:tomorrowString])
    {
        return @"明天";
    }
    else
    {
        return dateString;
    }
}


+ (NSString*)staticCompareDate:(NSString *)date
{
    return [[self alloc]compareDate:date];
}

+ (NSString*)staticWeekDayStr:(NSString *)date
{
    return [[self alloc]weekDayStr:date];
}

+ (NSString*)combinationCompareData:(NSString*)time
{
    
    NSString * dateString = [[self alloc]compareDate:time];
    
    if ([dateString isEqualToString:@"今天"] || [dateString isEqualToString:@"昨天"])
    {
        return dateString;
    }else{
        return [[self alloc] weekDayStr:time];
    }
    
    
}

// 判断是几月
- (NSString*)yearDayStr:(NSString *)date
{
    NSDate *today = [[NSDate alloc] init];
    NSString *todatString = today.description;
    
    if (todatString.length >= 10) {
        NSString *nowString = [todatString substringToIndex:10];
        
        NSString *varString = [date substringToIndex:10];
        
        NSArray *array = [nowString componentsSeparatedByString:@"-"];
        
        NSArray *varArray = [varString componentsSeparatedByString:@"-"];
        
        if (array.count == 0) {
            array = [nowString componentsSeparatedByString:@"/"];
            varArray = [varString componentsSeparatedByString:@"/"];
        }
        if (array.count >= 3) {
            NSInteger month = [[array objectAtIndex:1] integerValue];
            NSInteger Varmonth = [[varArray objectAtIndex:1] integerValue];
            
            if (month == Varmonth) {
                return @"本月";
            }else{
                return [NSString stringWithFormat:@"%ld月",(long)Varmonth];
            }
            
        }
    }
    return @"";
}

// 判断是几月
+ (NSString*)staticYearDayStr:(NSString *)date
{
    NSString *timeString = [[self alloc] yearDayStr:date];
    return timeString;
}


@end
